<template>
	<div id="paramInfo">
		<el-row style="margin-bottom: 20px;">
			<el-col :span="12" style="line-height:33px;">参数信息</el-col>
		</el-row>
		<Table
			:tableData="tableData"
			:tableHead="tableHead"
			:isUseSlot="false"
			:page="listQuery.page"
			:limit="listQuery.limit"
			v-loading="listLoading"
			:showIndex="false"
		>
			<template slot="tool" slot-scope="{ data }">
				<el-button
					style="padding:3px;"
					icon="el-icon-document"
					@click="handleEdit(data.row)"
					type="text"
					>编辑</el-button
				>
			</template>
		</Table>
		<Pagination
			v-show="total > 0"
			:total="total"
			:page.sync="listQuery.page"
			:limit.sync="listQuery.limit"
			@pagination="getList"
		></Pagination>
		<form-tem
			:info="{ title: '参数信息', width: '700px' }"
			:formList="formList"
			@submit="submit"
			ref="formDelg"
			@change="changeSource"
		></form-tem>
	</div>
</template>

<script>
import Table from "@/components/Table";
import Pagination from "@/components/Pagination"; // 分页
import {
	getSystemSetInfo,
	updateSystemSetInfo
} from "@/api/RCIM/RCIM-systemConfig";
import formTem from "@/template/form.temp";
import VForm from "@/components/luyiming/VueEleForm/index.vue";
export default {
	components: {
		Table,
		Pagination,
		formTem,
		VForm
	},
	data() {
		const validateNumber = (rule, value, callback) => {
			if (isNaN(value)) {
				callback(new Error("必须是数字"));
			} else {
				callback();
			}
		};
		const validateInt = (rule, value, callback) => {
			if (isNaN(value) || /\./.test(value)) {
				callback(new Error("必须是整数"));
			} else {
				callback();
			}
		};
		return {
			tableData: [],
			tableHead: [
				{
					prop: "statisticsTime",
					label: "统计时间",
					width: "",
					align: "center"
				},
				{
					prop: "minute",
					label: "处置时长阈值",
					width: "",
					align: "center"
				},
				{
					prop: "abnormalFactor",
					label: "异常系数",
					width: "",
					align: "center"
				},
				{
					prop: "superviseFactor",
					label: "督办系数",
					width: "",
					align: "center"
				},
				{
					prop: "operate",
					label: "操作",
					width: "",
					align: "center"
				}
			],
			listLoading: false,
			total: 0,
			listQuery: {
				page: 1,
				limit: 10
			},
			dialogFormVisible: false,
			formList: [
				{
					list: [
						{
							type: "input",
							width: "60%",
							label: "处置时间阈值（分钟）",
							labelWidth: "165px",
							showWordLimit: true,
							maxlength: "10",
							placeholder: "请输入",
							moduleKey: "minute",
							validate: function() {
								return [
									{
										required: true,
										message: "处置时间阈值不能为空",
										trigger: "change"
									},
									{
										validator: validateInt,
										trigger: "change"
									}
								];
							}
						}
					],
					title: "督办信息"
				},
				{
					list: [
						{
							type: "input",
							label: "统计时间",
							width: "60%",
							placeholder: "请输入",
							moduleKey: "statisticsTime",
							validate: function() {
								return [
									{
										required: true,
										message: "请选择统计时间",
										trigger: "change"
									},
									{
										validator: validateInt,
										trigger: "change"
									}
								];
							}
						}
					],
					title: "统计信息"
				},
				{
					list: [
						{
							type: "input",
							width: "60%",
							label: "异常系数",
							showWordLimit: true,
							maxlength: "5",
							placeholder: "请输入",
							moduleKey: "abnormalFactor",
							validate: function() {
								return [
									{
										required: true,
										message: "异常系数不能为空",
										trigger: "change"
									},
									{
										validator: validateNumber,
										trigger: "change"
									}
								];
							}
						},
						{
							type: "input",
							width: "60%",
							label: "督办系数",
							showWordLimit: true,
							maxlength: "5",
							placeholder: "请输入",
							moduleKey: "superviseFactor",
							validate: function() {
								return [
									{
										required: true,
										message: "督办系数不能为空",
										trigger: "change"
									},
									{
										validator: validateNumber,
										trigger: "change"
									}
								];
							}
						}
					],
					title: "算法信息"
				}
			]
		};
	},
	created() {
		this.getList();
	},
	methods: {
		/**
		 * @desc 获取表格数据
		 */
		async getList() {
			this.listLoading = true;
			const { data } = await getSystemSetInfo(this.listQuery);
			this.listLoading = false;
			console.log(data);
			this.tableData = [data.data];
		},
		/**
		 * @desc 编辑表格数据
		 * @param {Object} row 表格行数据
		 */
		handleEdit(row) {
			this.openDialog();
			let values = [{}, {}, {}];
			const first = ["minute"];
			const second = ["statisticsTime"];
			const third = ["abnormalFactor", "superviseFactor"];
			first.forEach(v => {
				values[0][v] = row[v];
			});
			second.forEach(v => {
				values[1][v] = row[v];
			});
			third.forEach(v => {
				values[2][v] = row[v];
			});
			this.$nextTick(() => {
				this.$refs.formDelg.getFormEle().forEach((item, ide) => {
					item[0].setDefaultValue(values[ide]);
				});
			});
		},
		/**
		 * @desc 编辑数据
		 * @param {Object} formData 表单数据
		 */
		submit(formData) {
			console.log(formData);
			updateSystemSetInfo(formData).then(res => {
				if (res.code === 200) {
					this.getList();
					this.closeDialog();
					return this.$message({
						type: "success",
						message: "编辑成功"
					});
				}
				return this.$message({
					type: "error",
					message: "编辑失败"
				});
			});
		},
		/**
		 * @desc 关闭弹窗
		 */
		closeDialog() {
			this.$refs.formDelg.closeDialog();
		},
		/**
		 * @desc 打开弹窗
		 */
		openDialog() {
			this.$refs.formDelg.openDialog();
		},
		/**
		 * @desc 表单联动函数
		 * @param {String} keys key
		 * @param {String} value 
		 */
		changeSource(keys, value) {
			// debugger
			if (keys === "abnormalFactor") {
				this.$refs.formDelg.getFormEle()[2][0].setDefaultValue({
					superviseFactor: this.accSub(1, value)
				});
			}

			if (keys === "superviseFactor") {
				this.$refs.formDelg.getFormEle()[2][0].setDefaultValue({
					abnormalFactor: this.accSub(1, value)
				});
			}
		},
		/**
		 * @desc 计算数据
		 * @param {Number} arg1 第一个字段值
		 * @param {Number} arg2 第二个字段值
		 */
		accSub(arg1, arg2) {
			var r1, r2, m, n;
			try {
				r1 = arg1.toString().split(".")[1].length;
			} catch (e) {
				r1 = 0;
			}
			try {
				r2 = arg2.toString().split(".")[1].length;
			} catch (e) {
				r2 = 0;
			}
			m = Math.pow(10, Math.max(r1, r2));
			//last modify by deeka
			//动态控制精度长度
			n = r1 >= r2 ? r1 : r2;
			return ((arg1 * m - arg2 * m) / m).toFixed(n);
		}
	}
};
</script>

<style lang="scss" scoped>
#paramInfo {
	height: calc(100vh - 144px);
	overflow: hidden;
	padding: 30px 50px;
}
</style>
